package com.lr.falsework.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ws.commons.constant.OperTypeEnum;
import com.ws.commons.interfac.Result;
import com.ws.mybatissmart.SmartMapper;
import com.ws.mybatissmart.WhereSql;

public class BaseService<E extends BaseEntity,M  extends SmartMapper<E>> {

	@Autowired
	protected M currentMapper;
	
	@Autowired
	protected SmartMapper<E> smartMapper;
	public List<E> selectByWhere(E e){
	return	currentMapper.selectByWhere(e, null);
	}
	public E selectOneByWhere(E e){
		List<E>  list=currentMapper.selectByWhere(e,null);
		if(list==null||list.size()==0) {
			return null;
		}else {
				return list.get(0);
		}
	}

	
	public Result add(E e){
		e.dftVal(OperTypeEnum.ADD);
		e.validVal(OperTypeEnum.ADD);
		currentMapper.insert(e);
		return Result.success();
	}
	public Result updateById(E e){
		e.dftVal(OperTypeEnum.UPDATE);
		e.validVal(OperTypeEnum.UPDATE);
		currentMapper.updateById(e);
		return Result.success();
	}
	
	
	public E selectById(Object idV,Class<E> cl){
		return currentMapper.selectById(idV,cl);
	}
	
	public Result deleteById(Object idV,Class<E> cl){
		 currentMapper.deleteById(idV,cl);
		 return Result.success();
	}
	
	public Result deleteByWhere(E e){
		currentMapper.deleteByWhere(e,null);
		return Result.success();
	}
	public Result deleteForWhere(WhereSql whereSql,Class<E> cl){
		currentMapper.deleteForWhere(cl, whereSql);
		return Result.success();
	}
}
